﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NETMovie.Entities;
using NETMovie.UI.WebMVC.Models;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using NETMovie.BLL;
using System.Web.Mvc;
using NETMovie.UI.WebMVC.Mappers;

namespace NETMovie.UI.WebMVC.Mappers
{
    public class MovieMapper
    {
        public MovieMapper()
        {
        }

        public WatchMovieModel ReMap(Movie movie)
        {
            WatchMovieModel model = new WatchMovieModel();
            model.Title = movie.Title;
            model.MovieWidth = 480;
            model.MovieHeight = 272;
            model.MovieID = movie.MovieId;
            model.MoviePath = movie.FileName;

            return model;
        }


        public Movie Map(MovieModel addModel, string RandomMovieFileName)
        {
            if (addModel == null)
                return null;

            Movie movie = new Movie();


            MovieCategory movieCategory = new MovieCategory();

            movieCategory.CategoryCode = addModel.Category;
            movie.Category = movieCategory;
            movie.Description = addModel.Description;
            movie.IsPremium = addModel.isPremium;
            movie.ReleaseDate = addModel.ReleaseDate;
            movie.AvailableDate = addModel.AvailableDate;
            movie.Summary = addModel.Summary;
            movie.Title = addModel.Title;
            movie.DeleteFlg = false;
            movie.FileName = RandomMovieFileName;


            return movie;
        }

        public Movie EditMovieMap(MovieModel editModel, string RandomMovieFileName)
        {


            Movie movie = new Movie();
            movie.MovieId = editModel.MovieId;

            MovieCategory movieCategory = new MovieCategory();

            movieCategory.CategoryCode = editModel.Category;
            movie.Category = movieCategory;
            movie.Description = editModel.Description;
            movie.IsPremium = editModel.isPremium;
            movie.ReleaseDate = editModel.ReleaseDate;
            movie.AvailableDate = editModel.AvailableDate;
            movie.Summary = editModel.Summary;
            movie.Title = editModel.Title;
            movie.FileName = RandomMovieFileName;
            return movie;
        }


        public MovieModel EditMovieReMap(Movie movie)
        {


            MovieModel editMovieModel = new MovieModel();


            editMovieModel.MovieId = movie.MovieId;
            editMovieModel.isPremium = movie.IsPremium;
            editMovieModel.ReleaseDate = movie.ReleaseDate;
            editMovieModel.Title = movie.Title;
            editMovieModel.Summary = movie.Summary;
            editMovieModel.Description = movie.Description;
            editMovieModel.AvailableDate = movie.AvailableDate;


            editMovieModel.Category = movie.Category.CategoryCode;
            //  Article[] articleArr = movie.Articles.ToArray();

            //   List<Article> articleList = new List<Article>;



            /*   foreach(var article in articleArr) 
                   {
                   editMovieModel.Articles.ToList().Add
                       (new SelectListItem {
                       Text = article.ArticleId;
                      Value =article.ArticleURL;
                       } 
                  );

                   }

             */

            // editMovieModel.CastMembers


            return editMovieModel;
        }

        public Movie MapMovieCategory(Movie movie, MovieCategory movieCategory)
        {
            movie.Category = movieCategory;
            return movie;
        }

        public IEnumerable<UserCommentModel> ReMap(UserComment[] userComments)
        {
            var comments = new List<UserCommentModel>(userComments != null ? userComments.Length : 3);
            if (userComments == null)
                return comments;
            foreach (var comment in userComments)
            {
                comments.Add(new UserCommentModel()
                {
                    UserName = "[TOFIX: get userName]", //comment.User.UserName.ToString(),
                    CommentText = comment.CommentText
                });
            }
            return comments;
        }

        public IEnumerable<MovieModel> ReMapSearchMovie(IEnumerable<Movie> movieList)
        {


            var movieSearchResults = new List<MovieModel>();
            if (movieList == null)
                return movieSearchResults;

            foreach (var movie in movieList)
            {

                MovieModel model = new MovieModel();

                model.Title = movie.Title;
                // model.Category = movie.CategoryReference;
                model.Description = movie.Description;
                model.Summary = movie.Summary;
                model.AvailableDate = movie.AvailableDate;
                model.MovieId = movie.MovieId;
                model.isPremium = movie.IsPremium;
                model.ReleaseDate = movie.ReleaseDate;
                model.MoviePath = movie.FileName;
                 
                
                movieSearchResults.Add(model);



            }
            return movieSearchResults;
        }
        public MovieModel ReMapDetail(Movie movie)
        {

            if (movie == null)
                return null;

            MovieModel model = new MovieModel();
           

            model.Title = movie.Title.ToString();
            // model.Category = movie.CategoryReference;
            model.Description = movie.Description.ToString();
            model.Summary = movie.Summary.ToString();
            model.AvailableDate = movie.AvailableDate;
            model.MovieId = movie.MovieId;
            model.isPremium = movie.IsPremium;
            model.ReleaseDate = movie.ReleaseDate;
            model.Category = movie.Category.CategoryName;
            return model;
        }

       public CastMember MapCastMember(CastMemberModel castmemberModel)
        {
           
            CastMember castmember= new CastMember();
            castmember.Name = castmemberModel.Name;
            castmember.Role = castmemberModel.Role;
            castmember.CastMemberId = castmemberModel.CastMemberId;
            return castmember;
        }


        public CastMemberModel [] ReMapCastMember( CastMember []  castMemberList, int movieId)
        {
            CastMemberModel [] castMemberModelList = new CastMemberModel[15];

              for(int i = 0; i <castMemberList.Length; i++)
            {
                  
                CastMemberModel castmemberModel = new CastMemberModel();
                castmemberModel.Name = castMemberList[i].Name;
                castmemberModel.MovieId = movieId;
                castmemberModel.Role = castMemberList[i].Role;
                castmemberModel.CastMemberId = castMemberList[i].CastMemberId;
                castMemberModelList[i] = castmemberModel;
            }

         for(int i = castMemberList.Length; i < castMemberModelList.Length; i++)
            {

                CastMemberModel castmemberModel =  new CastMemberModel();
                castmemberModel.MovieId = movieId;
                castMemberModelList[i] = castmemberModel;
            }
            return castMemberModelList;
        }


        

        public UserComment CommentMap(UserCommentModel userCommentModel)
        {

            if (userCommentModel == null)
                return null;

            UserComment userComment = new UserComment();
            userComment.CommentText = userCommentModel.CommentText;

            User user = new User();

            user.UserId = userCommentModel.UserId;
            userComment.User = user;
            //userComment.

         


            return userComment;
        }

        public Article mapArticle(MovieArticleModel artModel)
        {
            Article article = new Article();
           article.Title =artModel.Title;
           article.ArticleURL = artModel.ArticleURL;
           article.SourceName = artModel.SourceName;
           article.SourceURL = artModel.SourceURL;

            return article;
        }

        public MovieArticleModel ReMapArticle(Article article, int movieId)
        {
            MovieArticleModel articleModel = new MovieArticleModel();
           articleModel.Title =  article.Title;
           articleModel.ArticleURL = article.ArticleURL;
           articleModel.SourceName = article.SourceName;
           articleModel.SourceURL = article.SourceURL;
            articleModel.MovieId = movieId;
            return articleModel;
        }
        

        public IEnumerable<SelectListItem> MapMovieCategories(IEnumerable<MovieCategory> movieCategoryList)
        {


            return from s in movieCategoryList
                   orderby s.CategoryName
                   select new SelectListItem
                   {
                       Text = s.CategoryName,
                       Value = s.CategoryCode
                       //  Selected = (s.ID == ID)
                   };

        }

    }
}